package com.mentaleye.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.StudentPropertyLoanRecordBean;
import com.mentaleye.biz.StudentPropertyLoanRecordBiz;
import com.mentaleye.dal.model.Property;
import com.mentaleye.dal.model.StudentPropertyLoanRecord;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.service.PropertyService;
import com.mentaleye.service.StudentPropertyLoanRecordService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.DateFormatter;
import com.mentaleye.util.ObjectUtil;

@Service
public class StudentPropertyLoanRecordBizImpl implements StudentPropertyLoanRecordBiz {

	private Logger logger=Logger.getLogger(StudentPropertyLoanRecordBiz.class);
	
	@Autowired private PropertyService propertyService;
	
	@Autowired private StudentPropertyLoanRecordService recordService;
	
	@Override
	public CommonResult insertBean(StudentPropertyLoanRecordBean recordBean) {
		CommonResult result=new CommonResult();
		recordBean.setLoanTime(DateFormatter.parse(recordBean.getLoanTimeString(), "yyyy-MM-dd"));
		
		Property property=null;
		
		try {
			property=this.propertyService.getRecordByKey(recordBean.getPropertyId());
			if(property.getPropertyState()==DictionaryEnums.IN_USE.getCode()){
				result.setResultMsg("该财产未归还！");
			}
			else
			{
				property=new Property();
				property.setPropertyId(recordBean.getPropertyId());
				property.setPropertyState(DictionaryEnums.IN_USE.getCode());
				
				StudentPropertyLoanRecord record=(StudentPropertyLoanRecord) ObjectUtil.transfer(recordBean, StudentPropertyLoanRecord.class);
				if(this.propertyService.updateRecordBySelective(property)){
					if(this.recordService.insertRecord(record)){
						result.setIsSuccess(true);
						result.setResultMsg("添加财产借出记录成功！");
					}
					else{
						result.setResultMsg("添加财产借出记录失败！");
					}
				}
				else
				{
					result.setResultMsg("添加财产借出记录失败！");
				}	
			}
			
		} catch (Exception e) {
			logger.error("添加财产借出记录异常！", e);
		}
		return result;
	}

	@Override
	public CommonResult updateBean(StudentPropertyLoanRecordBean recordBean) {
		CommonResult result=new CommonResult();
		recordBean.setLoanTime(DateFormatter.parse(recordBean.getLoanTimeString(), "yyyy-MM-dd"));
		
		try {
			StudentPropertyLoanRecord record=(StudentPropertyLoanRecord) ObjectUtil.transfer(recordBean, StudentPropertyLoanRecord.class);
			if(this.recordService.updateRecord(record)){
				result.setIsSuccess(true);
				result.setResultMsg("修改财产借出记录成功！");
			}
			else{
				result.setResultMsg("修改财产借出记录失败！");
			}
		} catch (Exception e) {
			logger.error("修改财产借出记录异常！", e);
		}
		return result;
	}

	@Override
	public CommonResult deleteByList(List<Long> idList) {
		boolean isInUse=false;
		boolean deleteResult=false;
		boolean insertResult=false;
		CommonResult result=new CommonResult();
		List<String> propertyIdList=new ArrayList<String>();
		
		try {
			List<StudentPropertyLoanRecord> recordList = this.recordService.getListByKeys(idList);
			for (StudentPropertyLoanRecord record : recordList) {
				propertyIdList.add(record.getPropertyId());
			}
			
			List<Property> propertyList = this.propertyService.getListByKeys(propertyIdList);
			for (Property property : propertyList) {
				if(property.getPropertyState()==DictionaryEnums.IN_USE.getCode()){
					isInUse=false;
					break;
				}
				else
				{
					property.setPropertyState(DictionaryEnums.UNUSE.getCode());
				}
			}
			
			if(isInUse){
				result.setResultMsg("财产未归还！");
			}
			else
			{
				deleteResult=this.propertyService.deleteByList(propertyIdList);
				insertResult=this.propertyService.insertByList(propertyList);
				
				if(deleteResult&&insertResult){
					if(this.recordService.deleteByList(idList)){
						result.setIsSuccess(true);
						result.setResultMsg("删除财产借出记录成功！");
					}
					else{
						result.setResultMsg("删除财产借出记录失败！");
					}
				}
				else
				{
					result.setResultMsg("删除财产借出记录失败！");
				}
			}
			
		} catch (Exception e) {
			logger.error("删除财产借出记录异常！", e);
		}
		return result;
	}

	@Override
	public List<StudentPropertyLoanRecord> getList() {
		List<StudentPropertyLoanRecord> recordList=null;
		try {
			recordList=this.recordService.getList();
		} catch (Exception e) {
			logger.error("查询财产借出记录异常！", e);
		}
		return recordList;
	}

	@Override
	public List<StudentPropertyLoanRecordBean> getBeanList() {
		List<StudentPropertyLoanRecordBean> beanList=null;
		try {
			ObjectUtil.transferList(this.recordService.getList(), StudentPropertyLoanRecord.class);
		} catch (Exception e) {
			logger.error("查询财产借出记录异常！", e);
		}
		return beanList;
	}

	@Override
	public void getListByPage(CommonPage page) {
		try {
			page.setPageData(this.recordService.getListByMap(page.pageToMap()));
		} catch (Exception e) {
			logger.error("查询财产借出记录异常！", e);
		}
	}

	@Override
	public Map<String, Object> getMapById(long id) {
		Map<String, Object> resultMap=null;
		Map<String, Object> paramMap=new HashMap<String, Object>();
		paramMap.put("recordId", id);
		try {
			List<Map<String, Object>> mapList = this.recordService.getListByMap(paramMap);
			if(!mapList.isEmpty()){
				resultMap=mapList.get(0);
			}
		} catch (Exception e) {
			logger.error("查询财产借出记录异常！", e);
		}
		return resultMap;
	}
}
